package com.example.finalapplication;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

public class PlantsDetailActivity extends AppCompatActivity implements Runnable{
String href;
String plantsdetail;
Handler handler;
Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plants_detail);
        Intent intent7 =getIntent();
        String title=intent7.getStringExtra("title");
        Log.i(TAG,"title"+title);
        href=intent7.getStringExtra("href");
        Log.i(TAG,"hrefStr"+href);
        ((TextView)findViewById(R.id.plantsname)).setText(title);

        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    ((TextView)findViewById(R.id.plantsdetail)).setText(plantsdetail);
                    ((ImageView)findViewById(R.id.plantspic)).setImageBitmap(bmp);

                }
                super.handleMessage(msg);
            }

        };
         Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
         try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
        try {

            String url="http://db.kib.ac.cn/CNFlora/"+href;
            Document doc = Jsoup.connect(url).get();
            String title = doc.title();
            Log.i(TAG, "run:title=" + title);
            Elements pre = doc.getElementsByTag("pre");
            Elements imgs = doc.getElementsByTag("img");
            if(imgs.size()>1) {
                Element img = imgs.get(1);
            String Img=img.attr("src");
            URL urlpic= new URL(Img);
            HttpURLConnection conn = (HttpURLConnection) urlpic.openConnection();
            conn.setConnectTimeout(6000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(false);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);//读取图像数据
            is.close();


            Log.i(TAG,"img:"+Img);}
            else{
             String Img2=" http://db.kib.ac.cn/images/DCP-logo.jpg" ;
             URL urlpic2= new URL(Img2);
            HttpURLConnection conn = (HttpURLConnection) urlpic2.openConnection();
            conn.setConnectTimeout(6000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(false);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);//读取图像数据
            is.close();

            }
                for (Element spans : pre) {
                    Elements span = spans.getElementsByTag("span");
                    Log.i(TAG, "run:as.size=" + span.size());
                    if (span.size() > 0) {
                        plantsdetail = span.get(0).text();

                        Log.i(TAG, "run:" + plantsdetail);
                        //   HashMap<String, String> map = new HashMap<String, String>();
                        // map.put("plantsdetail", plantsdetail);
                        // retList.add(map);
                    }
                }


        } catch (IOException e) {
            e.printStackTrace();
        }


        Message msg = handler.obtainMessage(6);
        //msg.obj = "Hello";

        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage ok");
    }
    }
